package com.lewis.system.controller;

import com.lewis.core.annotation.Log;
import com.lewis.core.annotation.RepeatSubmit;
import com.lewis.core.base.controller.BaseController;
import com.lewis.core.base.domain.BaseResult;
import com.lewis.core.base.page.TableDataInfo;
import com.lewis.core.constant.UserConstants;
import com.lewis.core.enums.BusinessType;
import com.lewis.core.utils.SecurityUtil;
import com.lewis.core.utils.office.excel.ExcelUtil;
import com.lewis.system.entity.SysConfig;
import com.lewis.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author Lewis
 */
@Api(tags = "参数配置")
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {

	@Resource
	private ISysConfigService configService;

	@ApiOperation(value = "获取参数配置列表", notes = "获取参数配置列表")
	@GetMapping("/list")
	public TableDataInfo list(SysConfig config) {
		startPage();
		return getTableData(configService.selectConfigList(config));
	}

	@ApiOperation(value = "导出参数配置列表", notes = "导出参数配置列表")
	@Log(
			title = "参数管理",
			businessType = BusinessType.EXPORT
	)
	@GetMapping("/export")
	public BaseResult export(SysConfig config) {
		List<SysConfig> list = configService.selectConfigList(config);
		return new ExcelUtil<>(SysConfig.class).exportExcel(list, "参数数据");
	}

	@ApiOperation(value = "根据参数编号获取详细信息", notes = "根据参数编号获取详细信息")
	@GetMapping(value = "/{id}")
	public BaseResult getInfo(@PathVariable Long id) {
		return ok().setData(configService.selectConfigById(id));
	}

	@ApiOperation(value = "根据参数键名查询参数值", notes = "根据参数键名查询参数值")
	@GetMapping(value = "/configKey/{configKey}")
	public BaseResult getConfigKey(@PathVariable String configKey) {
		return ok().setData(configService.selectConfigByKey(configKey));
	}

	@ApiOperation(value = "新增参数配置", notes = "新增参数配置")
	@Log(
			title = "参数管理",
			businessType = BusinessType.INSERT
	)
	@PostMapping
	@RepeatSubmit
	public BaseResult add(@Validated @RequestBody SysConfig config) {
		if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
			return fail().setMsg("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
		}
		config.setCreateBy(SecurityUtil.getUsername());
		return getResult(configService.insertConfig(config));
	}

	@ApiOperation(value = "修改参数配置", notes = "修改参数配置")
	@Log(
			title = "参数管理",
			businessType = BusinessType.UPDATE
	)
	@PutMapping
	public BaseResult edit(@Validated @RequestBody SysConfig config) {
		if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
			return fail().setMsg("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
		}
		config.setUpdateBy(SecurityUtil.getUsername());
		return getResult(configService.updateConfig(config));
	}

	@ApiOperation(value = "删除参数配置", notes = "删除参数配置")
	@Log(
			title = "参数管理",
			businessType = BusinessType.DELETE
	)
	@DeleteMapping("/{ids}")
	public BaseResult remove(@PathVariable Long[] ids) {
		configService.deleteConfigByIds(ids);
		return ok();
	}

	@ApiOperation(value = "刷新参数缓存", notes = "刷新参数缓存")
	@Log(
			title = "参数管理",
			businessType = BusinessType.CLEAN
	)
	@DeleteMapping("/refreshCache")
	public BaseResult refreshCache() {
		configService.resetConfigCache();
		return ok();
	}
}
